package it.cast.wechat.service.ServiceImpl;

import it.cast.wechat.mapper.CategoryMapper;
import it.cast.wechat.pojo.Category;
import it.cast.wechat.pojo.CategoryExample;
import it.cast.wechat.service.CategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.UUID;

@Service
public class CategoryServiceImpl implements CategoryService {
    @Autowired
    private CategoryMapper categoryMapper;

    @Override
    public void addCategory(Category category) {
        if(StringUtils.isEmpty(category.getCategoryId())){
            category.setCategoryId(UUID.randomUUID().toString().replace("-",""));
        }
        categoryMapper.insert(category);
    }

    @Override
    public int modifyCategory(Category category) {
        int state = categoryMapper.updateByPrimaryKey(category);
        return state;
    }

    @Override
    public int deleteCategoryById(String categoryId) {
        int state = categoryMapper.deleteByPrimaryKey(categoryId);
        return state;
    }

    @Override
    public Category queryCategoryById(String categoryId) {
        Category category = categoryMapper.selectByPrimaryKey(categoryId);
        return category;
    }

    @Override
    public List<Category> queryAllCategory() {
        CategoryExample example = new CategoryExample();
        CategoryExample.Criteria criteria = example.createCriteria();
        criteria.andCategoryIdIsNotNull();
        List<Category> categories = categoryMapper.selectByExample(example);
        return categories;
    }
}
